﻿using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.Modularity;

namespace Linker.TaskCenter.EntityFrameworkCore
{
    [DependsOn(
        typeof(TaskCenterTestBaseModule),
        typeof(TaskCenterEntityFrameworkCoreModule),
        typeof(AbpEntityFrameworkCoreMySQLModule)
    )]
    public class TaskCenterEntityFrameworkCoreTestModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = BuildConfiguration();
            Configure<AbpDbContextOptions>(options =>
            {
                options.Configure(abpDbContextConfigurationContext =>
                {
                    abpDbContextConfigurationContext.DbContextOptions.UseMySql(
                        configuration.GetConnectionString("Default"),
                        new MySqlServerVersion(new Version(5, 7)));
                });
            });
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Linker.TaskCenter.HttpApi.Host/"))
                .AddJsonFile("appsettings.json", false);

            return builder.Build();
        }
    }
}